package com.focus.xyz.core.utils;

import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.NoSuchWindowException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import java.util.Set;

/**
 * Created by chenmeng on 2016/3/30.
 */
public class WindowUtils {


    public static boolean switchToWindow(WebDriver driver, String windowTitle) {
        boolean flag = false;
        try {
            String currentHandle = driver.getWindowHandle();
            Set<String> handles = driver.getWindowHandles();
            for (String s : handles) {
                if (s.equals(currentHandle))
                    continue;
                else {
                    driver.switchTo().window(s);
                    if (driver.getTitle().contains(windowTitle)) {
                        flag = true;
                        System.out.println("Switch to window: " + driver.getCurrentUrl() + " successfully!");
                        System.out.println("Debug-----" + driver.getWindowHandle());
                        break;
                    } else
                        continue;
                }
            }
        } catch (NoSuchWindowException e) {
            flag = false;
        }
        return flag;
    }

    public static void switchToWindowByUrl(WebDriver driver, String url){
        switchToWindowByUrl(driver,url,false);
    }

    public static void switchToWindowByUrl(WebDriver driver, String url,boolean isMax) {
        try {
            String currentHandle = driver.getWindowHandle();
            Set<String> handles = driver.getWindowHandles();
            for (String s : handles) {
                if (s.equals(currentHandle))
                    continue;
                else {
                    driver.switchTo().window(s);
                    if(isMax){
                        driver.manage().window().maximize();
                    }
                    if (driver.getCurrentUrl().contains(url)) {
                        System.out.println("Switch to window: "
                                + driver.getCurrentUrl() + " successfully!");
                        break;
                    } else
                        continue;
                }
            }
        } catch (NoSuchWindowException e) {
            System.out.println("切换异常：" + driver.getCurrentUrl());
        }
    }


    public static boolean closeWindowByTitle(WebDriver driver, String windowTitle) {
        boolean flag = false;
        try {
            String currentHandle = driver.getWindowHandle();
            Set<String> handles = driver.getWindowHandles();
            for (String s : handles) {
                if (s.equals(currentHandle))
                    continue;
                else {
                    WebDriver window = driver.switchTo().window(s);
                    if (window.getTitle().contains(windowTitle)) {
                        window.close();
                        flag = true;
                        break;
                    } else
                        continue;
                }
            }
        } catch (NoSuchWindowException e) {
            flag = false;
        }
        return flag;
    }

    public static boolean closeWindowByUrl(WebDriver driver, String url) {
        boolean flag = false;
        try {
            String currentHandle = driver.getWindowHandle();
            Set<String> handles = driver.getWindowHandles();
            for (String s : handles) {
                if (s.equals(currentHandle))
                    continue;
                else {
                    WebDriver window = driver.switchTo().window(s);
                    if (window.getCurrentUrl().contains(url)) {
                        window.close();
                        flag = true;
                        break;
                    } else
                        continue;
                }
            }
        } catch (NoSuchWindowException e) {
            System.out.println("关闭窗口异常：" + driver.getCurrentUrl());
            flag = false;
        }
        return flag;
    }


    public static void switchToParentWindow(WebDriver driver, String url) {
        try {
            Set<String> handles = driver.getWindowHandles();
            for (String s : handles) {
                driver.switchTo().window(s);
                System.out.println("all**********************:" + driver.getCurrentUrl());
                if (driver.getCurrentUrl().contains(url)) {
                    System.out.println("Switch to window: "
                            + driver.getCurrentUrl() + " successfully!");
                    break;
                } else
                    continue;
            }
        } catch (NoSuchWindowException e) {
            System.out.println("切换异常：" + driver.getCurrentUrl());
        }
        System.out.println("当前窗口URL:" + driver.getCurrentUrl());
    }

    public static boolean isAlertPresent(WebDriver driver) {
        try {
            driver.switchTo().alert();
            return true;
        } catch (NoAlertPresentException Ex) {
            return false;
        }
    }

    public static void switchToFrame(WebDriver driver,WebElement frame) throws Exception{
        driver.switchTo().frame(frame);
    }
}
